#include "UICheckBox.h"
#include "win_gdi.h"

UICheckBox::UICheckBox(UIContainer *parent) : UILabel(parent)
{
    setName("UICheckBox");
}

UICheckBox::~UICheckBox()
{
}

std::string UICheckBox::getUIType()
{
    return "UICheckBox";
}
void UICheckBox::setChecked(bool checked)
{
    if(m_Checked != checked){
        m_Checked = checked;
        // 单选，需要处理其他的选中情况
        if(m_Checked && !m_MultiSelect){
            auto parent = this->getParent();
            if(parent){
                for(int i=0;i<parent->getChildCount();i++){
                    auto child = parent->getChild(i);
                    if(child->getUIType() == this->getUIType() && child!= this){
                        auto checkBox = (UICheckBox*)child;
                        if(checkBox->getGroup() == this->getGroup()){
                            checkBox->setChecked(false);
                        }
                    }
                }
            }
        }
    }
}

bool UICheckBox::getChecked()
{
    return this->m_Checked;
}

void UICheckBox::onDrawContent(DuiWinManager *mgr, DuiRect &rect_in, DuiRect &rect_out)
{
    // 绘制复选框
    auto h = rect_in.h/2;
    auto rc = DuiRect{
        rect_in.x + 2,
        rect_in.y + h/2,
        h,
        h,
    };
    render_draw_rect(mgr, rc, m_Color, 1, DUI_LINE_STYLE_SOLID);
    if(m_Checked){
        auto rc2 = DuiRect{
            rc.x+2,
            rc.y+2,
            rc.w-3,
            rc.h-3,
        };
        render_draw_fill(mgr, rc2, m_Color);
    }

    // 文字
    auto rc3 = DuiRect{
        rc.x+rc.w + 4,
        rect_in.y,
        rect_in.w-h-4,
        rect_in.h,
    };
    std::wstring text;
    if(m_Checked){
        text = L"Checked";
    }else{
        text = L"Unchecked";
    }
    render_draw_text(mgr,rc3, text, m_Color, DUI_TEXT_ALIGN_LEFT, DUI_TEXT_ALIGN_CENTER, m_FontFamily, m_FontSize, m_FontWeight);
}

void UICheckBox::onMouseDown(int x, int y)
{
    this->setChecked(!this->getChecked());
    auto mgr = this->getWinManager();
    RECT rc;
    rc.left = this->m_Rect.x;
    rc.top = this->m_Rect.y;
    rc.right = this->m_Rect.x + this->m_Rect.w;
    rc.bottom = this->m_Rect.y + this->m_Rect.h;
    InvalidateRect((HWND)mgr->hWin, &rc, false);
}

